﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLib.Accounts.Bus;
using System.Data;
using DataLib.Common;

namespace Ewts.Storage
{
    public partial class StartStorage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserInfo"] != null)
                {
                    User curUser = new DataLib.Accounts.Bus.User();
                    curUser = (User)Session["UserInfo"];
                    if (curUser.UserType != "AD")
                    {
                        hidGraphic.Value = curUser.CompanyCity;
                    }
                    hidoperman.Value = curUser.UserName;
                    bindMonth();

                    if (Request["insid"] != null && Request["insid"].ToString().Trim() != "")
                    {
                        bindData(int.Parse(Request["insid"].ToString().Trim()));
                    }
                    else
                    {
                        
                        if (Request.Cookies["pdid"] != null)
                        {
                            string pdid = Request.Cookies["pdid"].Value;
                            bindData(int.Parse(pdid));
                        }
                    }


                }
                else
                {
                    Response.Redirect("/accounts/login.aspx");
                }

            }
        }

        protected void bindData(int insid)
        {
            DataLib.BLL.BarCode bll = new DataLib.BLL.BarCode();

            DataLib.Model.BarCode bar = new DataLib.Model.BarCode();
            DataLib.Model.InStorage ins = new DataLib.BLL.InStorage().GetModel(insid);
            if (ins != null)
            {
                DataTable dt = bll.GetList("InStrorageID=" + insid.ToString()).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    bar = bll.GetModel(int.Parse(dt.Rows[0]["id"].ToString().Trim()));
                    if (bar != null)
                    {
                        hidCJOID.Value = bar.cjoid.ToString();
                        hidLoadID.Value = bar.loadnumid.ToString();
                        hidInStoID.Value = bar.InStrorageID.ToString();
                        txtLoadNo.Value = bar.loadnumv;
                    }
                    reptStartStorage.DataSource = dt;
                    reptStartStorage.DataBind();
                }
            }
            
        }

        protected void bindMonth()
        {

            int year = DateTime.Now.Year;
            //txtInputDate.Value = DateTime.Now.ToShortDateString();
            ListItem listitem = new ListItem((year + 1).ToString() + "年", (year + 1).ToString());
            dropYear.Items.Add(listitem);
            listitem = new ListItem(year.ToString() + "年", year.ToString());
            dropYear.Items.Add(listitem);
            listitem = new ListItem((year - 1).ToString() + "年", (year - 1).ToString());
            dropYear.Items.Add(listitem);

            dropYear.SelectedValue = year.ToString();
            dropMonth.SelectedValue = DateTime.Now.Month.ToString().PadLeft(2, '0');

        }

        ///称重提交事件
        protected int? SubmitInWrl()
        {
            DataLib.BLL.InStorage bllIn = new DataLib.BLL.InStorage();
            DataLib.Model.InStorage inModel = new DataLib.Model.InStorage();
            DataLib.BLL.VehicleArrange bllVA = new DataLib.BLL.VehicleArrange();
            DataLib.Model.VehicleArrange vaModel = new DataLib.Model.VehicleArrange();
            inModel = bllIn.GetModel(int.Parse(hidInStoID.Value));
            bllIn.Update(inModel);
            string month = dropYear.SelectedValue + dropMonth.SelectedValue;
            bllIn.InstorageSubmit((int)inModel.id, "6",month);
            return inModel.id;

        }

        protected void btnSaveSubmit_Click(object sender, EventArgs e)
        {
            if (hidInStoID.Value == "" || hidInStoID.Value == "0")
            {
                MessageBox.Show(this, "不能提交空的收集清单");
                return;
            }


            int? wrlid = SubmitInWrl();
            DataLib.Common.MessageBox.ShowAndRedirect(Page, "保存成功！", "/RDLC/PDReport.aspx?insid=" + wrlid.ToString());
        }

    }
}
